<?php
namespace app\api\controller;

use app\api\Base;
use think\Config;
use think\Db;
use baseutils\BaseUtil; # 验证类
use com\gdl\newagk\interfaces\QbankServiceClient;
use com\gdl\newagk\dataobject\qbank\QbankDTO; 

/**
 * App题库接口
 * @author tc 2018/05/07
 */

class Qbank extends Base
{
 	/** 
 	 *  app题库列表 
 	 *  @return JSON [description] 400失败 200成功
 	 */
	public function qbankList() 
    {
    	$msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : ""; # 当前平台id
        $identity_id = $this->request->param('identity_id') ? $this->request->param('identity_id') : ""; # 用户id
        $liekName = $this->request->param('name') ? $this->request->param('name') : ""; #题库名称
        $tag = $this->request->param("tag") ?  $this->request->param("tag") : ""; # 标签pid
        $tagVal = $this->request->param("tagVal") ?  $this->request->param("tagVal") : ""; # 标签值
        $offset = $this->request->param('page') ? $this->request->param('page') : 0; # 分页
        $num = $this->request->param('pagesize') ? $this->request->param('pagesize') : 10; # 条数
        if(empty($plaformId)){
            $msg['msg'] = "请带入平台标识";
            return json_encode($msg);
        }
        if(empty($identity_id)){
            $msg['msg'] = "请带入用户标识";
            return json_encode($msg);
        }
        # 获取该平台下子平台和当前平台的数据
        $adminReg = getApiAdminReg($plaformId,'one');

        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $QbankService = new QbankServiceClient(NULL);
            \ApiClient::build($QbankService);
            $QbankDO = new QbankDTO();  
            
            $data = ['admin_reg'=>$adminReg,'name'=>$liekName,'tag'=>$tag,'tagVal'=>$tagVal];
            $QbankDO->identity_id = $identity_id;
            $QbankDO->data = $data;
            $QbankDO->id = $plaformId;
            $QbankDO->filed = $adminReg;
            $QbankDO->offset = $offset;
            $QbankDO->num=$num;
            $result = $QbankService->qbankList($QbankDO);   
            if($result->success == true){
            	$msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                $msg['data'] = $result->data; 
            } 

        } catch (Exception $ex) {
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }

    /** 
     *  app题库详情 
     *  @return JSON [description] 400失败 200成功
     */
    public function qbankInfo() 
    {
        $msg = ['code'=>400, 'msg'=>'暂无数据', 'data'=>[]];
        $plaformId = $this->request->param('plaformId') ? $this->request->param('plaformId') : 0; # 平台id
        $qbankId = $this->request->param('qbankid') ? $this->request->param('qbankid') : 0; # 题库id
        $identityId = $this->request->param("identityId")?$this->request->param("identityId"):""; # 用户identity
        try { 
            \ApiClient::init(Config::get('api.appid'), Config::get('api.secret'), 30, array('gdlApiSdk','gdlCloudSdk'));
            $QbankService = new QbankServiceClient(NULL);
            \ApiClient::build($QbankService);
            $QbankDO = new QbankDTO(); 
            $QbankDO->id = $plaformId;
            $QbankDO->userid = $qbankId; 
            $QbankDO->gdl_userid = $identityId;
            $result = $QbankService->qbankInfo($QbankDO); 
            if ($result->success == TRUE) {
                $msg['msg'] = $result->message;
                $msg['code'] = $result->code;
                if($result->code==200){
                    $msg['data'] = $result->data[0]; 
                }
                
            } else {
                SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('error', $result->message);
            }

        } catch (Exception $ex) {
            SLogger::getLogger('pub_thrift_service', 'thrift_error')->error('thrift call error', $ex);
        }
        return json_encode($msg);
    }

    /**
     * 当前平台所有标签
     */
    public function qbankTag(){
        $msg = ['code'=>400, 'msg'=>"暂无标签" , 'data'=>[]];
        $plaformId = $this->request->param("plaform_id") ?  $this->request->param("plaform_id") : "";
        $qbankId = $this->request->param("qbankid") ? $this->request->param("qbankid"): "";
        if(empty($plaformId)){
            $msg['msg'] = "请带入正确的平台标识";
            return json_encode($msg);
        }
        $msg['code'] = 200;
        $msg['data']['tagList'] = [];
        $msg['data']['sectionList'] = []; 

        try{
         
            $plafromWhere['isdelete'] = 0;
            $plafromWhere['type'] = 0;
            $list = Db::name('qbank_tag')->where($plafromWhere)->field("id,pid,tagName")->select(); 
            if($list){
                $newList = $this->getTree($list,0);
                $where['isdelete'] = 0;
                $where['status'] = 1;
                // $where['qbankId'] = $qbankId;
                $sectionList = Db::name('sections')->where($where)->field("id,seName")->select();
                if($newList){
                    $msg['code'] = 200;
                    $msg['msg'] = "获取成功";
                    $msg['data']['tagList'] = $newList;
                    $msg['data']['sectionList'] = $sectionList;
                } 
            }
        }catch(Exception $ex){
            $msg['msg'] = $ex->getMessage();
        }
        return json_encode($msg);
    }

    /**
     * 获取该题库的章节
     */
    public function getSectionList(){
        $msg = ['code'=>400, 'msg'=>'暂无题库章节', 'data'=>[]];
        $qbankId = $this->request->param("qbankid") ? $this->request->param("qbankid"): "";
        if(empty($qbankId)){
            $msg['msg'] ="请带入正确的题库标识";
            return json_encode($msg);
        }
        try{
            $where['isdelete'] = 0;
            $where['status'] = 1;
            $where['qbankId'] = $qbankId;
            $sectionList = Db::name('sections')->where($where)->field("id,seName")->select(); 
            if($sectionList){
                $msg['code'] = 200;
                $msg['msg'] = "获取成功";
                $msg['data'] = $sectionList;
            }

        }catch(Exception $e){
            $msg['msg'] = $e->getMessage();
        }
        return json_encode($msg);
    }

    /**
    *  找儿子
    */
    public static function getTree($data, $pId){
        $tree = '';
        foreach($data as $k => $v)
        {
          if($v['pid'] == $pId)
          {        //父亲找到儿子
            $v['pid'] = self::getTree($data, $v['id']) ? self::getTree($data, $v['id']) : [];
            $tree[] = $v; 
          } 
        }
        return $tree;
    }
    
    
}
